﻿using System;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;

public partial class sysadmin_DL_Project_AddRegion : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOAdvancedUser objAU = new TOAdvancedUser();
    TOProvince objPro = new TOProvince();
    TOCity objCity = new TOCity();
    TORegion objReg = new TORegion();
    VOTOCity voCity = new VOTOCity();
    VOTORegion voReg = new VOTORegion();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            int CID = 0;
            int RegID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                //BinderForm(ProID);
            }
            voCity = objCity.Load(CID);
            //BinderManager();
            BinderProvince();
            if (voCity != null)
                BinderCity(voCity.ProID,CID);
            else
                BinderCity(0,CID);

            if (Request.QueryString["RegID"] != null)
            {
                RegID = int.Parse(Request.QueryString["RegID"].ToString());
                ViewState["RegID"] = RegID.ToString();
                BinderForm(RegID);
            }

        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup != 1)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    //private void BinderManager()
    //{
    //    DataTable dt = new DataTable();

    //    dt = objAU.Query("AUserGroup<3");
    //    ddl_manager.DataSource = dt;
    //    ddl_manager.DataTextField = "Name";
    //    ddl_manager.DataValueField = "AUser_Id";
    //    ddl_manager.DataBind();  
    //}
    private void BinderProvince()
    {
        DataTable dt = new DataTable();

        dt = objPro.Query("");
        ddl_Province.DataSource = dt;
        ddl_Province.DataTextField = "Province";
        ddl_Province.DataValueField = "ID";
        ddl_Province.DataBind();
        if (voCity != null)
            ddl_Province.SelectedValue = voCity.ProID.ToString();
    }
    private void BinderCity(int pid,int cid)
    {
        DataTable dt = new DataTable();
        if(pid>0)
            dt = objCity.Query("ProID="+pid.ToString());
        else
            dt = objCity.Query("");


        ddl_City.DataSource = dt;
        ddl_City.DataTextField = "City";
        ddl_City.DataValueField = "ID";
        ddl_City.DataBind();
        if(cid!=0)
            ddl_City.SelectedValue = cid.ToString();
    }

    private void BinderForm(int id)
    {
        voReg = objReg.Load(id);
        txtNewTypeName.Text = voReg.Region;
        //ddl_manager.SelectedValue = voReg.ManagerID.ToString();
        ddl_City.SelectedValue = voReg.CityID.ToString();
        //.SelectedValue = vo.ProID.ToString();
        //BinderProvince(ViewState["ProID"].ToString());
    }
    
    private bool CheckPageData()
    {
        string search = string.Format("Region = '{0}'", txtNewTypeName.Text);
        if (ViewState["RegID"] != null)
        {
            search += "and ID<>" + ViewState["RegID"].ToString();
        }
        if (objReg.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        if (ViewState["RegID"] == null)
        {
            voReg.Region = txtNewTypeName.Text.Trim();
            voReg.ManagerID = 0;
            voReg.CityID = int.Parse(ddl_City.SelectedValue);
            voReg.CreatorId = GetLoginEntity.UserID;
            voReg.CreatorName = GetLoginEntity.UserName;
            if (objReg.Add(voReg))
            {
                
                voCity = objCity.Load(voReg.CityID);
                voCity.RegionNum=voCity.RegionNum+1;
                objCity.Update(voCity);
                string url = "ManageRegion.aspx?CID="+voReg.CityID.ToString();
                Script.RedirectURL(url);
            }
        }
        else
        {
            voReg=objReg.Load(int.Parse(ViewState["RegID"].ToString()));
            voReg.Region = txtNewTypeName.Text.Trim();
            voReg.ManagerID = 0;
            voReg.CityID = int.Parse(ddl_City.SelectedValue);
            if (objReg.Update(voReg))
            {
                string url = "ManageRegion.aspx?CID=" + voReg.CityID.ToString();
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageRegion.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);
    }

    protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderCity(int.Parse(ddl_Province.SelectedValue), 0);
    }
}
